<?php
Database($_SESSION['cid']);
header("content-type:text/html;charset=utf-8");
class DepartmentAction extends Action{
//查看部门列表
	public function index(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);		
		$data = pageing('audience_department',10,'',$_GET['seach'],'department_id desc','time_insert>time_delete','department_name','index',"DB_MEETING");		
		foreach($data['data'] as $value){
			$a_id .= ','.$value['leader_id'];
			$pid .= ','.$value['department_pid'];			
		}		
		unset($value);
		$pid = substr($pid,1);		
		$a = M('audience_department','','DB_MEETING')->where(array('department_id'=>array('in',$pid)))->select();
		foreach($a as $val){
			$data_pid[$val['department_id']] = $val;
		}
		unset($val);
		$a_id = substr($a_id,1);
		$id['audience_id'] = array("in",$a_id);		
		$audience = M('audience','','DB_MEETING')->where($id)->select();		
		foreach($audience as $value){			
			$aud[$value['audience_id']] = $value;
		}		
		unset($value);	
		$this->assign("data",$data['data']);
		$this->assign("audience",$aud);
		$this->assign("pid",$data_pid);
		$this->assign("page",$data['show']);		
		$this->display();
	}
	//部门搜索
	public function search(){
		if($_POST){
			$depart = M('audience_department','','DB_MEETING')->join("meeting_roomlocal on audience_department.local_id=meeting_roomlocal.local_id")->join("meeting_roombuilding on audience_department.building_id=meeting_roombuilding.building_id")->join("meeting_roomfloor on audience_department.floor_id = meeting_roomfloor.floor_id")->field("audience_department.*,meeting_roomlocal.local_name,meeting_roombuilding.building_name,meeting_roomfloor.floor_name")->select();

			$data = pageing('audience_department',4,'',$_POST['search'],'department_id desc','1','department_name','',"DB_MEETING");
			foreach($data['data'] as $k=>$val){
				$totals[$k] = M('audience','',"DB_MEETING")->where(array('audience_department'=>$val['department_id']))->select();
			}
			foreach($totals as $key=>$value){
				$num[$key] = count($value);
			}
		//方便分页
			foreach($depart as $k=>$v){
				$depart[$v['department_id']]=$v;
			}

		};

		$this->assign("data",$data['data']);
		$this->assign("page",$data['show']);
		$this->assign("depart",$depart);
		$this->assign("num",$num);
		$this->display();
	}
	
//部门添加
	public function forAct(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$city = D('Company/Department')->get_local();
		$build = D('Company/Department')->get_build();
		$floor = D('Company/Department')->get_floor();
		$departments = D('Department')->get_depart();
		$this->assign("city",$city);
		$this->assign("build",$build);
		$this->assign("floor",$floor);
		$this->assign('departments',$departments);
		$this->display();	
	}

//城市添加
	public function add_city(){		
		if($_POST['local_name']){
			$post['local_name']=$_POST['local_name'];
			$city_add = D('Company/Department')->add_local($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>添加失败,点击回到编辑页面</div>";
			}
		}
	}
	
//楼座添加
	public function add_build(){		
		if($_POST['building_name']){
			$post['building_name']=$_POST['building_name'];
			$city_add = D('Company/Department')->add_build($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>添加失败,点击回到编辑页面</div>";
			}
		}
	}
//楼层添加
	public function add_floor(){		
		if($_POST['floor_name']){
			$post['floor_name']=$_POST['floor_name'];
			$city_add = D('Company/Department')->add_floor($post);
			if($city_add){
				echo "<div class='back btn btn-info' style='float:right;font-size:20px;'>添加成功,点击回到编辑页面</div>";
			}else{
				echo "<div class='back btn btn-danger' style='float:right;font-size:20px;'>添加失败,点击回到编辑页面</div>";
			}
		}
	}
	
//添加部门
	public function addAct(){	
		if($_POST){
			$_POST['time_insert'] = time();
			$_POST['time_update'] = time();
			$_POST['time_delete'] = -1;
			$data = D('Company/Audience')->add_data('audience_department',$_POST);
			if($data){
				$this->success("部门添加成功",__APP__."/Department");
			}else{
				$this->error('添加失败!',__APP__."/Department/forAct");
			}
		}
	
	}

//删除部门
	public function delAct(){	
		$data = M('audience_department','','DB_MEETING')->where(array('department_pid'=>$_POST['department_id']))->find();
		$post['time_delete'] = time();		
		if(!empty($data)){
			$rspid = M('audience_department','','DB_MEETING')->where(array('department_pid'=>$_POST['department_id']))->save($post);
			if($rspid){
				$rs = D('Company/Audience')->save_data('audience_department','department_id',$_POST['department_id'],$post);
			}
		}else{
			$rs = D('Company/Audience')->save_data('audience_department','department_id',$_POST['department_id'],$post);
		}		
		if ($rs){
			echo '{"state":1}';
		}else{
			echo '{"state":0}';
		}
    }

//修改部门
	public function to_update(){		
		if($_GET){
			$departments = D('Department')->get_depart();
			$department  = D('Company/Department')->find_depart($_GET['department_id']);
			$city 		 = D('Company/Department')->get_local();
			$build 		 = D('Company/Department')->get_build();
			$floor 		 = D('Company/Department')->get_floor();
			$department['personnel'] = D('Department')->get_department_personnel($_GET['department_id']);
			$this->assign("city",$city);
			$this->assign("build",$build);
			$this->assign("floor",$floor);			
			$this->assign('department',$department);
			$this->assign('departments',$departments);
			$this->display();
		}
		
	}
//修改
	public function uptAct(){
		if($_POST)
			$_POST['time_update'] = time();
			$rs = D('Company/Audience')->save_data('audience_department','department_id',$_POST['department_id'],$_POST);
		if($rs){
			if($_POST['personnel']!='')
			{
				D('Department')->update_department_personnel($_POST);
			}
			$this->success("Update Success!",__APP__."/Department/index");
		} else {
			$this->error("update Fail for database");
		}
	}
	
//地址管理
	public function address(){
		$name=D('Company/Audience')->get_admin($_SESSION['id']);
		$this->assign("name",$name);
		$sql = "select * from meeting_roomlocal";
		//$local = M
		$sql = "SELECT meeting_roomlocal.*,meeting_roombuilding.building_name,meeting_roomfloor.* FROM `meeting_roomlocal` LEFT JOIN meeting_roombuilding on meeting_roomlocal.local_id = meeting_roombuilding.local_id LEFT JOIN meeting_roomfloor on meeting_roombuilding.building_id = meeting_roomfloor.floor_id";		
		$room = M('meeting_roomlocal','','DB_MEETING')->query($sql);
		$this->assign("room",$room);
		$this->display();
	}
	
}
?>